package com.xb.report.controller;


import com.github.pagehelper.Page;
import com.xb.report.service.IDataCountService;
import com.xb.report.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author gxd
 * jar运行 注意new modelAndView 开头没有/
 */

@Controller
@RequestMapping(value = "home")
public class HomeController {

    @Autowired
    IDataCountService dataCountService;

    Logger logger=LoggerFactory.getLogger(this.getClass());


    /**
     * 获取首页统计量数据
     * startDate--endDate
     * @return
     */
    @RequestMapping(value = "/getDatas")
    public @ResponseBody
    Map<String, Object> getDatas(HttpServletRequest request, HttpServletResponse res) {
        res.setHeader("Access-Control-Allow-Origin", "*");
        ModelMap map = new ModelMap();
        Integer r = null;
        String date=request.getParameter("date");
        String planType=request.getParameter("planType");

        String startDate="";
        String endDate="";

        if(Tools.notEmpty(date)){//日期不为空
            String [] strs=date.split(",");
            startDate=strs[0];
            endDate=strs[1];
        }else{//默认30天
            endDate=getEndDate();
            startDate=getStartDate();
        }

        List<Map<String, Object>> viewList= null;
        List<Map<String, Object>> visitList= null;
        List<Map<String, Object>> numList= null;
        List<Map<String, Object>> dedctionList= null;
        List<Map<String, Object>> payList= null;
        List<Map<String, Object>> profitList= null;

        List<Map<String, Object>> uidList= null;
        List<Map<String, Object>> planList= null;
        List<Map<String, Object>> advList= null;



        try {
            viewList = dataCountService.getViewsList(startDate,endDate,planType);
            visitList= dataCountService.getVisitList(startDate,endDate);
            numList = dataCountService.getNumList(startDate,endDate,planType);
            dedctionList= dataCountService.getDedctionList(startDate,endDate,planType);
            payList= dataCountService.getPayList(startDate,endDate,planType);
            profitList= dataCountService.getProfitList(startDate,endDate,planType);
            //首页从结果表result_出
            uidList= dataCountService.getUidTopFromResult(startDate,endDate,planType);
            planList= dataCountService.getPlanTopFromResult(startDate,endDate,planType);
            advList= dataCountService.getAdvTopFromResult(startDate,endDate,planType);

        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        map.put("viewList", viewList);//浏览量
        map.put("visitList", visitList);//访问量
        map.put("jsList", numList);//结算量
        map.put("kouList", dedctionList);//扣量
        map.put("payList", payList);//应付金额
        map.put("profitList", profitList);//盈利

        map.put("uidList", uidList);//站长top10
        map.put("planList", planList);//计划top10
        map.put("advList", advList);//广告主top10
        return map;
    }



    /**
     * 计划统计页 数据
     * startDate--endDate
     * @return
     */
    @RequestMapping(value = "/getPlanDatas")
    public @ResponseBody
    Map<String, Object> getPlanDatas(HttpServletRequest request, HttpServletResponse res) {
        res.setHeader("Access-Control-Allow-Origin", "*");
        ModelMap map = new ModelMap();
        Integer r = null;
        String date=request.getParameter("date");
        String planType=request.getParameter("planType");

        //计划统计 条件
        String planid=request.getParameter("planid");
        //站长统计 条件
        String uid=request.getParameter("uid");
        //广告统计 条件
        String adsid=request.getParameter("adsid");
        //广告位统计 条件
        String zoneid=request.getParameter("zoneid");
        //广告商统计 条件
        String advuid=request.getParameter("advuid");

        String startDate="";
        String endDate="";
        if(Tools.notEmpty(date)){//日期不为空
            String [] strs=date.split(",");
            startDate=strs[0];
            endDate=strs[1];
        }else{//默认30天
            endDate=getEndDate();
            startDate=getStartDate();
        }

        List<Map<String, Object>> phoneBrandList= null;//待修改
        List<Map<String, Object>> browserList= null;//浏览器分布
        List<Map<String, Object>> ispList= null;//isp分布
        List<Map<String, Object>> areaList= null;//区域分布
        List<Map<String, Object>> planList= null;//计划结算量top10
        List<Map<String, Object>> advList= null;//广告投放top10
        List<Map<String, Object>> zoneList= null;//广告位结算top10
        List<Map<String, Object>> uidList= null;//站长结算top10
        List<Map<String, Object>> advuidList= null;//广告商结算top10
        List<Map<String, Object>> siteList= null;//网站结算top10

        try {
            //饼状图_钻取  排除广告商id advuid
            if(Tools.notEmpty(planid) ||Tools.notEmpty(uid) ||Tools.notEmpty(adsid) ||Tools.notEmpty(zoneid)){
                phoneBrandList= dataCountService.getPhoneBrandListByids(planid,uid,adsid,zoneid,advuid,startDate,endDate);
                browserList= dataCountService.getBrowserListByids(planid,uid,adsid,zoneid,advuid,startDate,endDate);
                ispList= dataCountService.getIspListByids(planid,uid,adsid,zoneid,advuid,startDate,endDate);
                areaList= dataCountService.getAreaListByids(planid,uid,adsid,zoneid,advuid,startDate,endDate);
            }else{
                phoneBrandList= dataCountService.getPhoneBrandList(startDate,endDate);
                browserList= dataCountService.getBrowserList(startDate,endDate);
                ispList = dataCountService.getIspListList(startDate,endDate);
                areaList = dataCountService.getAreaListList(startDate,endDate);
            }


            if(Tools.notEmpty(planid)){
                uidList= dataCountService.getUidTop10(planid,adsid,advuid,startDate,endDate,planType);
                advList= dataCountService.getPlanAdvTop10(planid,uid,zoneid,startDate,endDate,planType);
                zoneList= dataCountService.getZoneTop10(planid,uid,adsid,startDate,endDate,planType);
            }else if(Tools.notEmpty(uid)){
                planList= dataCountService.getPlanTop10(uid,startDate,endDate,planType);
                advList= dataCountService.getPlanAdvTop10(planid,uid,zoneid,startDate,endDate,planType);
                zoneList= dataCountService.getZoneTop10(planid,uid,adsid,startDate,endDate,planType);
            }else if(Tools.notEmpty(adsid)){
                zoneList= dataCountService.getZoneTop10(planid,uid,adsid,startDate,endDate,planType);
                uidList= dataCountService.getUidTop10(planid,adsid,advuid,startDate,endDate,planType);
            }else if(Tools.notEmpty(zoneid)){
                advuidList=dataCountService.getAdvTop10(zoneid,startDate,endDate,planType);
                advList= dataCountService.getPlanAdvTop10(planid,uid,zoneid,startDate,endDate,planType);
            }else if(Tools.notEmpty(advuid)){
                uidList= dataCountService.getUidTop10(planid,adsid,advuid,startDate,endDate,planType);
                siteList=dataCountService.getSiteTop10(advuid,startDate,endDate,planType);
            }else{
                uidList= dataCountService.getUidTopFromResult(startDate,endDate,planType);
                planList= dataCountService.getPlanTopFromResult(startDate,endDate,planType);
                advList= dataCountService.getPlanAdvTopFromResult(startDate,endDate,planType);
                zoneList= dataCountService.getZoneTopFromResult(startDate,endDate,planType);
                advuidList=dataCountService.getAdvTopFromResult(startDate,endDate,planType);
                siteList=dataCountService.getSiteTopFromResult(startDate,endDate,planType);
            }

        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        map.put("phoneBrandList", phoneBrandList);
        map.put("browserList", browserList);
        map.put("areaList", areaList);
        map.put("ispList", ispList);


        map.put("planList", planList);
        map.put("advList", advList);
        map.put("zoneList", zoneList);
        map.put("uidList", uidList);
        map.put("advuidList", advuidList);
        map.put("siteList", siteList);


        return map;
    }




    //======================数据统计相关=========================

    //======================计划统计=====================
    @RequestMapping(value = "/count/getPlanListByPage")
    public @ResponseBody
    Map<String, Object> getPlanListByPage(HttpServletRequest request, HttpServletResponse res) {
        res.setHeader("Access-Control-Allow-Origin", "*");
        ModelMap map = new ModelMap();
        Integer r = null;

        String planId=request.getParameter("planId");
        String planType=request.getParameter("planType");
        String sort=request.getParameter("sort");

        String date=request.getParameter("date");
        String startDate="";
        String endDate="";

        if(Tools.notEmpty(date)){//日期不为空
            String [] strs=date.split(",");
            startDate=strs[0];
            endDate=strs[1];
        }else{//默认30天
            endDate=getEndDate();
            startDate=getStartDate();
        }

        int pageNum= Integer.parseInt(request.getParameter("pageNum"));
        int pageSize=Integer.parseInt(request.getParameter("pageSize"));

        Page<Map<String, Object>> page= dataCountService.getPlanListByPage(planId,planType,sort,startDate,endDate,pageNum, pageSize);
        //查询各项总数 站长统计_汇总
        List<Map<String, Object>> sumPlanList= dataCountService.getSumPlanList(planId,planType,startDate,endDate);


        map.put("number", page.getTotal());
        map.put("pagesdata", page);
        map.put("sumPlanList", sumPlanList);
        return map;
    }


    //======================站长统计=====================
    @RequestMapping(value = "/count/getUidListByPage")
    public @ResponseBody
    Map<String, Object> getUidListByPage(HttpServletRequest request, HttpServletResponse res) {
        res.setHeader("Access-Control-Allow-Origin", "*");
        ModelMap map = new ModelMap();
        Integer r = null;

        String uid=request.getParameter("uid");
        String planType=request.getParameter("planType");
        String sort=request.getParameter("sort");

        String date=request.getParameter("date");
        String startDate="";
        String endDate="";

        if(Tools.notEmpty(date)){//日期不为空
            String [] strs=date.split(",");
            startDate=strs[0];
            endDate=strs[1];
        }else{//默认30天
            endDate=getEndDate();
            startDate=getStartDate();
        }

        int pageNum= Integer.parseInt(request.getParameter("pageNum"));
        int pageSize=Integer.parseInt(request.getParameter("pageSize"));

        Page<Map<String, Object>> page= dataCountService.getUidListByPage(uid,planType,sort,startDate,endDate,pageNum, pageSize);
        //查询各项总数 站长统计_汇总
        List<Map<String, Object>> sumUidList= dataCountService.getSumUidList(uid,planType,startDate,endDate);



        map.put("number", page.getTotal());
        map.put("pagesdata", page);
        map.put("sumUidList", sumUidList);
        return map;
    }

    //======================广告统计=====================
    @RequestMapping(value = "/count/getAdsIdListByPage")
    public @ResponseBody
    Map<String, Object> getAdsIdListByPage(HttpServletRequest request, HttpServletResponse res) {
        res.setHeader("Access-Control-Allow-Origin", "*");
        ModelMap map = new ModelMap();
        Integer r = null;

        String adsid=request.getParameter("adsid");
        String planType=request.getParameter("planType");
        String sort=request.getParameter("sort");

        String date=request.getParameter("date");
        String startDate="";
        String endDate="";

        if(Tools.notEmpty(date)){//日期不为空
            String [] strs=date.split(",");
            startDate=strs[0];
            endDate=strs[1];
        }else{//默认30天
            endDate=getEndDate();
            startDate=getStartDate();
        }

        int pageNum= Integer.parseInt(request.getParameter("pageNum"));
        int pageSize=Integer.parseInt(request.getParameter("pageSize"));

        Page<Map<String, Object>> page= dataCountService.getAdsIdListByPage(adsid,planType,sort,startDate,endDate,pageNum, pageSize);
        //查询各项总数 广告统计_汇总
        List<Map<String, Object>> sumAdsIdList= dataCountService.getSumAdsIdList(adsid,planType,startDate,endDate);



        map.put("number", page.getTotal());
        map.put("pagesdata", page);
        map.put("sumAdsIdList", sumAdsIdList);
        return map;
    }


    //======================广告位统计=====================
    @RequestMapping(value = "/count/getZoneIdListByPage")
    public @ResponseBody
    Map<String, Object> getZoneIdListByPage(HttpServletRequest request, HttpServletResponse res) {
        res.setHeader("Access-Control-Allow-Origin", "*");
        ModelMap map = new ModelMap();
        Integer r = null;

        String zoneid=request.getParameter("zoneid");
        String planType=request.getParameter("planType");
        String sort=request.getParameter("sort");

        String date=request.getParameter("date");
        String startDate="";
        String endDate="";

        if(Tools.notEmpty(date)){//日期不为空
            String [] strs=date.split(",");
            startDate=strs[0];
            endDate=strs[1];
        }else{//默认30天
            endDate=getEndDate();
            startDate=getStartDate();
        }

        int pageNum= Integer.parseInt(request.getParameter("pageNum"));
        int pageSize=Integer.parseInt(request.getParameter("pageSize"));

        Page<Map<String, Object>> page= dataCountService.getZoneIdListByPage(zoneid,planType,sort,startDate,endDate,pageNum, pageSize);
        //查询各项总数 广告位统计_汇总
        List<Map<String, Object>> sumZoneIdList= dataCountService.getSumZoneIdList(zoneid,planType,startDate,endDate);


        map.put("number", page.getTotal());
        map.put("pagesdata", page);
        map.put("sumZoneIdList", sumZoneIdList);
        return map;
    }

    //======================广告商统计=====================
    @RequestMapping(value = "/count/getAdvUidListByPage")
    public @ResponseBody
    Map<String, Object> getAdvUidListByPage(HttpServletRequest request, HttpServletResponse res) {
        res.setHeader("Access-Control-Allow-Origin", "*");
        ModelMap map = new ModelMap();
        Integer r = null;

        String advuid=request.getParameter("advuid");
        String planType=request.getParameter("planType");
        String sort=request.getParameter("sort");

        String date=request.getParameter("date");
        String startDate="";
        String endDate="";

        if(Tools.notEmpty(date)){//日期不为空
            String [] strs=date.split(",");
            startDate=strs[0];
            endDate=strs[1];
        }else{//默认30天
            endDate=getEndDate();
            startDate=getStartDate();
        }

        int pageNum= Integer.parseInt(request.getParameter("pageNum"));
        int pageSize=Integer.parseInt(request.getParameter("pageSize"));

        Page<Map<String, Object>> page= dataCountService.getAdvUidListByPage(advuid,planType,sort,startDate,endDate,pageNum, pageSize);
        //查询各项总数 广告商统计_汇总
        List<Map<String, Object>> sumAdvUidList= dataCountService.getSumAdvUidList(advuid,planType,startDate,endDate);


        map.put("number", page.getTotal());
        map.put("pagesdata", page);
        map.put("sumAdvUidList", sumAdvUidList);
        return map;
    }


    /**
     * 获取YYYY-MM-DD格式 前一天
     *
     * @return
     */
    private String getEndDate() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date today=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        today = calendar.getTime();
        return sdf.format(today);
    }
    private  String getStartDate() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date today=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);
        calendar.add(Calendar.DAY_OF_MONTH, -30);
        today = calendar.getTime();
        return sdf.format(today);
    }



    /*public static  void main(String args[]){
        Pattern pattern = Pattern.compile(";\\s?(\\S*?\\s?\\S*?)\\s?(Build)?/");
        String s1="Mozilla/5.0 (Mozilla/5.0 (Linux; Android 4.2.2; HUAWEI G750-T00 Build/HuaweiG750-T00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044109 Mobile Safari/537.36 MicroMessenger/6.6.7.1321(0x26060739) NetType/WIFI Language/zh_CN";
        String s2="Mozilla/5.0 (Linux; Android 8.1.0; zh-CN; EML-AL00 Build/HUAWEIEML-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.0.3.983 Mobile Safari/537.36";
        String s3="Mozilla/5.0 (Linux; Android 7.0; STF-AL10 Build/HUAWEISTF-AL10; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044109 Mobile Safari/537.36 MicroMessenger/6.6.7.1321(0x26060739) NetType/WIFI Language/zh_CN";
        String s4="Mozilla/5.0 (Linux; Android 6.0.1; SUGAR Y9 Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044109 Mobile Safari/537.36 MicroMessenger/6.6.7.1321(0x26060739) NetType/WIFI Language/zh_CN";
        String s5="Mozilla/5.0 (Linux; Android 4.4.4; vivo X5Max+ Build/KTU84P; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044109 Mobile Safari/537.36 MicroMessenger/6.6.7.1321(0x26060739) NetType/WIFI Language/zh_CN";
        String s6="Mozilla/5.0 (Linux; Android 7.1.1; OPPO R11 Build/NMF26X; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044109 Mobile Safari/537.36 MicroMessenger/6.6.6.1300(0x26060638) NetType/WIFI Language/zh_CN";


        List<String> list =new ArrayList<String>();
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        list.add(s5);
        list.add(s6);

        for (String s: list) {
            Matcher matcher = pattern.matcher(s);
            String model = null;
            if (matcher.find()) {
                model = matcher.group(1).trim();
                System.out.println("通过userAgent解析出机型：" + model);
            }
        }

    }*/

}
